const AdminBiz = require('../../../../../../comm/biz/admin_biz.js');
const pageHelper = require('../../../../../../helper/page_helper.js');
const helper = require('../../../../../../helper/helper.js');
const RequestBiz = require('../../../../../../comm/biz/request_biz.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		activityId: '',
		title: '',
		titleEn: '',
		searchColumn: 'showName',
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		if (!AdminBiz.isAdmin(this)) return;

		// 附加参数 
		if (options && options.activityId) {
			this.setData({
				_params: { activityId: options.activityId},
				isLoad: true,
				fields: 'id,activityId,userId,addTime,userPic,showName,userName,'
			});
		}

		if (options && options.title) {
			let title = decodeURIComponent(options.title);
			this.setData({
				title,
				titleEn: options.title,
				maxCnt: options.maxCnt
			});
			wx.setNavigationBarTitle({
				title: '活动名单 - ' + title
			});
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	url: async function (e) {
		pageHelper.url(e, this);
	},

	bindCommListCmpt: function (e) {
		if (helper.isDefined(e.detail.search))
			this.setData({
				search: '',
				sortType: '',
			});
		else {
			let dataList = e.detail.dataList;

			this.setData({
				dataList,
			});
		}
	},

	bindDelTap: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let id = pageHelper.dataset(e, 'id');
		let params = {
      id,
      userName: 'admin'
    }
    
		let callback = async () => {
			RequestBiz.request('/api/ysjl/activity/deleteEnroll', params).then(res => {
        let index = e.currentTarget.dataset.idx;
        let dataList = this.data.dataList;
        dataList.list.splice(index, 1);
        if (dataList.otherList && dataList.otherList.length > 0) {
          let otherData = dataList.otherList.shift();
          dataList.list.push(otherData);
        }
        this.setData({
          dataList: dataList
        });
				pageHelper.showSuccToast('删除成功');
			});
		}
		pageHelper.showConfirm('确认删除？删除后报名数据将一并删除且不可恢复', callback);

  },
  
	bindDelUnderStudyTap: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let id = pageHelper.dataset(e, 'id');
		let params = {
      id,
      userName: 'admin'
    }
    
		let callback = async () => {
			RequestBiz.request('/api/ysjl/activity/deleteEnroll', params).then(res => {
				pageHelper.delListNode(id, this.data.dataList.otherList, 'id');
				this.setData({
					dataList: this.data.dataList
				});
				pageHelper.showSuccToast('删除成功');
			});
		}
		pageHelper.showConfirm('确认删除？删除后报名数据将一并删除且不可恢复', callback);

	},
})